package com.lagou.mr.number_sort;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class NumberSortReducer extends Reducer<NumberBean, NullWritable, NullWritable, Text> {
    private Integer i = 1;
    Text text = new Text();
    @Override
    protected void reduce(NumberBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        // 为了避免compareTo方法导致总播放时长相等的对象被当成相等，而合并了key所以遍历values获取每个key(bean对象)
        for (NullWritable value : values) {
            text.set(i + "\t" + key.getNumber());
            context.write(NullWritable.get(), text);
            i++;
        }
    }
}
